import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val=val;
    }
}

public class BinaryTree {

//    根据输入字符串创建树
    public static int i=0;
    public TreeNode createBinaryTree(String str) {
        TreeNode root=null;
        if(str.charAt(i)!='#') {
            root=new TreeNode(str.charAt(i));
            i++;
            root.left=createBinaryTree(str);
            root.right=createBinaryTree(str);
        }else {
            //空树
            i++;
        }
        return root;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root==null) return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root==null) return;
        System.out.print(root.val+" ");
        inOrder(root.left);
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if(root==null) return;
        System.out.print(root.val+" ");
        postOrder(root.left);
        postOrder(root.right);
    }

    //无返回值版层序遍历
    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null) return;
        queue.offer(root);
        while(!queue.isEmpty()) {
            //若当前队列非空，弹出队头节点给cur
            TreeNode cur=queue.poll();
            //打印cur
            System.out.print(cur.val+" ");
            //将cur的左右子节点放入队列（非空）
            if(cur.left!=null) queue.offer(cur.left);
            if(cur.right!=null) queue.offer(cur.right);
        }
        System.out.println();
    }

    //返回二维数组版层序遍历
    /*


    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ret=new ArrayList<>();
        if(root==null) return ret;

        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            int size=queue.size();
            List<Integer> list=new ArrayList<>();

            while(size>0) {
                TreeNode cur=queue.poll();
                list.add(cur.val);
                if(cur.left!=null) queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);
                size--;
            }
            ret.add(list);
        }
        return ret;
    }*/



//    判断一棵树是否是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if(root==null) return true;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur=queue.poll();
            if(cur==null) break;
            //若cur不为null，将cur的左右节点都放进来，null夜访
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        //此时cur为空，开始判断队列此时是否有非空的节点
        while(!queue.isEmpty()) {
            TreeNode node=queue.peek();
            if(node!=null) return false;
            else queue.poll();
        }
        return true;
    }


//    求二叉树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q) {
        if(root==null) return null;
        if(root==p || root==q) return root;

        TreeNode leftTree=lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);

        //leftTree和rightTree的返回值有以下几种情况
        if(leftTree!=null && rightTree!=null) return root;//p，q一个在左边一个在右边
        else if(leftTree!=null)return leftTree;//两个都在左边
        else return rightTree;//都在右边
    }


    //根据前序，中序创建二叉树
    /*
        preIndex标记前序数组遍历到的位置
        rootIndex表示中序数组中找到的根节点的位置
    */
    /*

    public int preIndex;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }

    private TreeNode buildTreeChild(int[] preorder,int[] inorder,int inBegin,int inEnd) {
        if(inBegin>inEnd) return null;
        TreeNode root=new TreeNode(preorder[preIndex]);
        int rootIndex=findRootIndex(inorder,inBegin,inEnd,preorder[preIndex]);
        preIndex++;
        root.left=buildTreeChild(preorder,inorder,inBegin,rootIndex-1);
        root.right=buildTreeChild(preorder,inorder,rootIndex+1,inEnd);
        return root;
    }

    private int findRootIndex(int[] inorder,int inBegin,int inEnd,int key) {
        for(int i=inBegin;i<=inEnd;i++) {
            if(inorder[i]==key)return i;
        }
        return -1;
    }

     */


    //根据后序，中序创建二叉树
    /*

    public int postIndex=0;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        postIndex=postorder.length-1;
        return buildTreeChild(inorder,postorder,0,inorder.length-1);
    }

    private TreeNode buildTreeChild(int[] inorder,int[] postorder,int inBegin,int inEnd) {
        if(inBegin>inEnd)return null;
        TreeNode root=new TreeNode(postorder[postIndex]);
        int rootIndex=findRootIndex(inorder,inBegin,inEnd,postorder[postIndex]);
        postIndex--;
        root.right=buildTreeChild(inorder,postorder,rootIndex+1,inEnd);
        root.left=buildTreeChild(inorder,postorder,inBegin,rootIndex-1);
        return root;
    }

    private int findRootIndex(int[] inorder,int inBegin,int inEnd,int key) {
        for(int i=inBegin;i<=inEnd;i++) {
            if(inorder[i]==key)return i;
        }
        return -1;
    }

     */




    //根据二叉树创建字符串
    public String tree2str(TreeNode root) {
        StringBuilder stringBuilder =new StringBuilder();
        tree2strChild(root,stringBuilder);
        return stringBuilder.toString();
    }

    private void tree2strChild(TreeNode root,StringBuilder stringBuilder) {
        if(root==null)return;
        stringBuilder.append(root.val);

        if(root.left!=null) {
            stringBuilder.append("(");
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(")");
        }else {
            //左边为空，右边也为空
            if(root.right==null) {
                return;
            }else {
                //左边为空，右边不为空
                stringBuilder.append("()");
            }
        }
        //处理root右树的情况
        if(root.right==null) {
            return;
        }else {
            stringBuilder.append("(");
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(")");
        }
    }


}
